package org.jgs1904.apachepoidemo.Algorithm;

import org.springframework.util.StringUtils;

import java.util.Random;

/**
 * @author: 默苍璃
 * @date: 2024-01-11 15:19
 * <p>
 * 插入排序
 */
public class InsertionSort {

    public static void main(String[] args) {

        Random random = new Random();
        int numberOfRecords = 100000;

        int[] array = new int[numberOfRecords];
        for (int i = 0; i < numberOfRecords; i++) {
            int randomNumber = random.nextInt(); // 生成一个随机整数
            array[i] = randomNumber;
        }

        long l = System.currentTimeMillis();
        // int[] array = {1, 55, 155, 133, 144, 233, 13, 4, 2, 4, 6, 55, 64, 212, 44, 33, 63};
        // 正序
        for (int i = 0; i < array.length - 1; i++) {
            int j = i;
            while (true) {
                if (j != -1 && array[j + 1] < array[j]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    j--;
                } else {
                    break;
                }
            }
        }

        for (int i : array) {
            System.out.println(i);
        }

        System.out.println("耗时");
        System.out.println(System.currentTimeMillis() - l);

        System.out.println("__________________________________");

        // l = System.currentTimeMillis();
        // int[] array1 = {1, 55, 155, 133, 144, 233, 13, 4, 2, 4, 6, 55, 64, 212, 44, 33, 63};
        // // 逆序
        // for (int i = 0; i < array1.length - 1; i++) {
        //     int j = i;
        //     while (true) {
        //         if (j + 1 > 0 && array1[j + 1] > array1[j]) {
        //             int temp = array1[j];
        //             array1[j] = array1[j + 1];
        //             array1[j + 1] = temp;
        //             j--;
        //         } else {
        //             break;
        //         }
        //     }
        // }
        //
        // for (int i : array1) {
        //     System.out.println(i);
        // }
        // System.out.println("耗时");
        // System.out.println(System.currentTimeMillis() - l);

    }

}
